<template>
	<view class="content">
		<view class="address-box">
			<view class="address-item" v-for="(item,index) in addressList" :key="index" >
				<view class="address-area" @click="handleSelect(item)">
					<view class="color-999 size-14">
						{{item.area}}
					</view>
					<view class="defaultText" v-if="item.defaultsOn">默认</view>
				</view>
				<view class="color-333 size-16 size-bold address-text" @click="handleSelect(item)">{{item.house}}</view>
				<view class="address-name" @click="handleSelect(item)">
					<view class="color-333 size-16 weight-500">{{item.name}}</view>
					<view class="color-999 size-14">{{item.phone}}</view>
				</view>
				<view class="address-bottom">
					<view class=""></view>
					<!-- 操作按钮 -->
					<view class="address-btn">
						<view @click="handleDelete(index)">删除</view>
						<view @click="handleCopy(item)">复制</view>
						<view @click="handleEdit(item)">修改</view>
					</view>
				</view>
			</view>
		</view>
		<view class="addAddress">
			<view class="addAddress-item" @click="handleToNew">
				<view class=""><uv-icon name="edit-pen" color="#fff" size="26"></uv-icon></view>
				<view class="color-ff size-16 ">新增收货地址</view>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: [{
						name: '张先生',
						phone: '15123520963',
						area: '浙江省 杭州市 上城区',
						house: '杭州市3-1-1号',
						defaultsOn: false
					},
					{
						name: '王女生',
						phone: '15123520988',
						area: '山西省 太原市 小店区',
						house: '太原市1-5',
						defaultsOn: false
					},
					{
						name: '刘先生',
						phone: '15123520963',
						area: '北京市 北京市 东城区',
						house: '观音桥3-1-1号',
						defaultsOn: true
					},
					{
						name: '吴女生',
						phone: '15123520988',
						area: '黑龙江省 哈尔滨市 道里区',
						house: '哈尔滨市 1-5',
						defaultsOn: false
					}
				],
				selectedAddressId: null
			};
		},
		onLoad(options) {
			// 判断结算页面是否选择地址
			this.isSpecial = options.fromSpecial === '1';
		},
		methods: {
			handleToNew() {
				// 新增地址逻辑
				uni.navigateTo({
					url: "/pages/me/MyAddress/newAddress/newAddress"
				});
			},
			handleSelect(item) { //切换地址
				if (this.isSpecial) {
					uni.navigateBack({
						delta: 1,
						success: () => {
							uni.$emit('addressSelected', item);
						}
					});
				}
			},
			handleCopy(item) { //复制地址
				const {
					defaultsOn,
					...addressToCopy
				} = item;
				const addressText =
					`${addressToCopy.area} ${addressToCopy.house} ${addressToCopy.name} ${addressToCopy.phone}`;
				uni.setClipboardData({
					data: addressText,
					success: () => {
						uni.showToast({
							title: '地址已复制',
							icon: 'success'
						});
					},
					fail: (err) => {
						console.error('复制失败', err);
						uni.showToast({
							title: '复制失败，请重试',
							icon: 'none'
						});
					}
				});
			},
			handleEdit(item) { //修改
				const params = encodeURIComponent(JSON.stringify(item));

				uni.navigateTo({
					url: `/pages/me/MyAddress/newAddress/newAddress?item=${params}`
				});
			}

		}
	}
</script>

<style lang="less">


	.address-box {
		padding: 30rpx;
		padding-bottom: calc(90rpx + env(safe-area-inset-bottom));

		.address-item {
			width: 100%;
			padding: 30rpx;
			margin-bottom: 30rpx;
			box-sizing: border-box;
			background-color: #fff;
			border-radius: 20rpx;

			.address-area {
				display: flex;
				align-items: center;
				gap: 20rpx;

				.defaultText {
					padding: 2rpx 12rpx;
					box-sizing: border-box;
					border: 1px solid #32b9b3;

					color: #32b9b3;
					font-size: 12px;

				}
			}

			.address-text {
				margin: 20rpx 0rpx;
			}

			.address-name {
				display: flex;
				gap: 20rpx;
			}

			.address-bottom {
				margin-top: 40rpx;
				padding: 20rpx 0rpx;
				border-top: 1px solid #F0F0F0;
				display: flex;
				align-content: center;
				justify-content: space-between;

				.address-btn {
					display: flex;
					align-items: center;
					gap: 30rpx;
					justify-content: space-between;
					color: #666;
					font-size: 16px;
				}
			}

		}
	}

	.addAddress {
		width: 100%;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		padding: 40rpx 30rpx;

		.addAddress-item {
			width: 90%;
			background-image: linear-gradient(to right, #7cdfe8, #32b9b3);
			background-color: #32b9b3;
			border-radius: 50rpx;
			padding: 20rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: center;
			align-content: center;
			gap: 10rpx;
		}
	}
</style>